package com.example.flowable.controller;

import com.example.flowable.vo.RequestParamsVo;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/api")
public class TestController {
    @Resource
    TaskService taskService;
    @Resource
    RuntimeService runtimeService;

    /**
     * 调试节点
     * @param demoParams
     * @return
     */
    @PostMapping(value = "/complete", produces = "application/json;charset=UTF-8")
    public String debug(@RequestBody RequestParamsVo demoParams) {
        // a997ecbc-505d-11ed-bd83-f09e4a5e8c75
        String taskId = demoParams.getTaskId();
        if (StringUtils.isEmpty(taskId)) {
            taskId = "a997ecbc-505d-11ed-bd83-f09e4a5e8c75";
        }
        Map<String, Object> variablesMap = new HashMap<>();
        variablesMap.put("isDebugger", true);
        taskService.complete(taskId, variablesMap);
        // TODO 返回信息，便于下次调用
        return "success";
    }

    /**
     * 启动执行流程实例
     * @param demoParams
     * @return
     */
    @PostMapping(value = "/executeProcessInstance", produces = "application/json;charset=UTF-8")
    public String executeProcessInstance(@RequestBody RequestParamsVo demoParams) {
        Map<String, Object> variables = new HashMap<>();
        variables.put("isDebugger", false);
//        runtimeService.startProcessInstanceById(demoParams.getProcessDefinitionKey(), variables);
        runtimeService.startProcessInstanceByKey(demoParams.getProcessDefinitionKey(), variables);
        // TODO 返回执行信息，便于查看执行结果
        return "success";
    }



}
